<template>
  <div class="page-content">
    <Table
      :listData="dataList"
      :listCount="listCount"
      @selectionChange="handleSelectionChange"
      v-bind="contentTableConfig"
      v-model:page="pageInfo"
    >
      <!--1. header中插槽-->
      <template #headerHandler>
        <el-button
          v-if="isCreate"
          type="primary"
          size="small"
          @click="handleNewClick"
          >添加</el-button
        >
      </template>

      <!--2. 列中插槽-->
      <template #status="scope">
        <el-button size="small" :type="scope.row.enable ? 'success' : 'danger'"
          >{{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
      <template #createAt="scope">
        <span>{{ $filters.formatTime(scope.row.createAt) }} </span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatTime(scope.row.updateAt) }} </span>
      </template>

      <template #handler="scope">
        <div class="handle-btns">
          <el-button
            v-if="isUpdate"
            size="small"
            @click="handleEditClick(scope.row)"
            >编辑</el-button
          >
          <el-button
            v-if="isDelete"
            size="small"
            type="danger"
            @click="handleDeleteClick(scope.row)"
            >删除</el-button
          >
        </div>
      </template>
      <!-- 动态插入剩下的插槽-->
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"> </slot>
        </template>
      </template>
    </Table>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref, watch } from 'vue'
import { useStore } from '@/store'
import Table from '@/base-ui/table'
import { usePermission } from '@/hooks/use-permission'

export default defineComponent({
  name: 'pageContent',
  components: { Table },
  props: {
    contentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  emits: ['newBtnClick', 'editClick'],
  setup(props, { emit }) {
    const store = useStore()

    //获取操作权限判断
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')

    //双向绑定pageInfo
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    watch(pageInfo, () => {
      getPageData()
    })

    //发送请求获取数据
    const getPageData = (queryInfo: any = {}) => {
      if (!isQuery) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()

    //获取vuex数据
    const dataList = computed(() =>
      store.getters[`system/pageListData`](props.pageName)
    )
    const listCount = computed(() =>
      store.getters[`system/pageListCount`](props.pageName)
    )

    const otherPropSlots = props.contentTableConfig?.propList.filter(
      (item: any) => {
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )

    const handleSelectionChange = (value: any) => {
      console.log(value)
    }
    const handleDeleteClick = (row: any) => {
      console.log(row)
      store
        .dispatch('system/deletePageDataAction', {
          pageName: props.pageName,
          id: row.id
        })
        .then((res) => {
          console.log(res)
        })
    }
    const handleEditClick = (row: any) => {
      emit('editClick', row)
      // console.log(row)
    }
    const handleNewClick = () => {
      emit('newBtnClick')
      // console.log('add')
    }

    return {
      dataList,
      listCount,
      handleSelectionChange,
      getPageData,
      pageInfo,
      otherPropSlots,
      isCreate,
      isUpdate,
      isDelete,
      handleDeleteClick,
      handleEditClick,
      handleNewClick
    }
  }
})
</script>

<style scoped>
.page-content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
